<?php
//接收版块id
$cid = $_GET['cid'];
?>
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>帖子列表</title>
		<link href="css/list.css" rel="stylesheet" type="text/css">
		<link href="css/public.css" rel="stylesheet" type="text/css">
	</head>
	<body>
		<?php 
			include './includes/header.inc.php';
		?>
		<!--帖子列表-->
		<div id="ft"><a href="post.php?cid=<?php echo $cid?>">发表普通帖子</a> | <a href="gpost.php?cid=<?php echo $cid?>">发表购买贴</a></div>
		<div id="list">
			<div class="list_top1">
				<div class="list_top2">帖子标题</div>
				<div class="list_top3">作者</div>
				<div class="list_top4">回复/查看</div>
				<div class="list_top5">最后发表</div>
			</div>
			
			<div class="list_content">
				<?php
					//当前页
					$page = isset($_GET['page']) ? $_GET['page'] : 1;
					//每页显示几条
					$pagerows = 6;
					//从哪里开始显示
					$offset = ($page - 1) * $pagerows;
					//拼接limit子句
					$limit = "limit {$offset},{$pagerows}";
					
					//准备查询帖子sql
					$sql = "select ftiezi.id,ftiezi.uid,user.username,cate.cname,ftiezi.title,ftiezi.content,ftiezi.rtime,ftiezi.fujian,ftiezi.money,ftiezi.high from ftiezi,user,cate where ftiezi.uid=user.id and ftiezi.cid=cate.id and ftiezi.huishou=0 and ftiezi.cid={$cid} {$limit}";
					//执行sql
					$res = mysqli_query($link, $sql);
					//判断执行结果
					if($res && mysqli_num_rows($res)){
						while($tinfo = mysqli_fetch_assoc($res)){	
				?>
				<div class="list_content_1">
					<div class="list_top2">
					<?php
					if($tinfo['money'] != 0){
						//查询用户是否购买
						$usql = "select uid,fid from isgoumai where uid={$_SESSION['id']} and fid={$tinfo['id']}";
						//执行sql
						$ures = mysqli_query($link, $usql);
						//判断执行结果
						if($ures && mysqli_num_rows($ures)){
							if($tinfo['high'] == 0){
						?>
								<a href="detail.php?tid=<?php echo $tinfo['id']?>&cid=<?php echo $cid?>"><?php echo $tinfo['title']?></a>
							<?php
								}else{
							?>
								<a href="detail.php?tid=<?php echo $tinfo['id']?>&cid=<?php echo $cid?>" style="color:red"><?php echo $tinfo['title']?></a>
								
					<?php
								}
						}else{
							echo '此贴是购买贴,需'.$tinfo['money'].'人名币<a href="goumai.php?cid='.$cid.'&umoney='.$info['money'].'&gmmoney='.$tinfo['money'].'&uid='.$tinfo['uid'].'&page='.$page.'&tid='.$tinfo['id'].'">购买</a>才能看';
						}
					}else{
						if($tinfo['high'] == 0){
					?>
						<a href="detail.php?tid=<?php echo $tinfo['id']?>&cid=<?php echo $cid?>"><?php echo $tinfo['title']?></a>
					<?php
						}else{
					?>
						<a href="detail.php?tid=<?php echo $tinfo['id']?>&cid=<?php echo $cid?>" style="color:red"><?php echo $tinfo['title']?></a>
					<?php
						}
					}
					?>
					</div>
					<div class="list_top3"><?php echo $tinfo['username'];?></div>
					<div class="list_top4">10/20</div>
					<div class="list_top5">
					<?php 
						if(time()-($tinfo['rtime']) < 60){
							echo '<span style="color:red;">'.(time()-($tinfo['rtime'])).'秒前</span>';
						}elseif(time()-($tinfo['rtime']) < 3600){
							echo '<span style="color:red;">'.floor((time()-($tinfo['rtime'])) / 60).'分钟前</span>';
						}elseif(time()-($tinfo['rtime']) < 86400){
							echo '<span style="color:red;">'.floor((time()-($tinfo['rtime'])) / 3600).'小时前</span>';
						}elseif(time()-($tinfo['rtime']) > 86400){
							echo '<span style="color:red;">'.floor((time()-($tinfo['rtime'])) / 86400).'天前</span>';
						}
					?>
					</div>
				</div>
				<?php
						}
					//释放结果集
					mysqli_free_result($res);
					}
				?>
			</div>
		</div>
		<div class="clear"></div>
		<?php
			//查询总条数
			$csql = "select count(*) from ftiezi where cid={$cid} and huishou=0";
			//执行sql
			$cres = mysqli_query($link, $csql);
			//判断执行结果
			if($cres && mysqli_num_rows($cres)){
				$c = mysqli_fetch_row($cres);
			//释放结果集
			mysqli_free_result($cres);
			}
			//总页数 = ceil(总条数 / 每页显示的条数)
			$cpage = ceil($c[0] / $pagerows);
			//上一页
			$prevpage = $page - 1;
			if($prevpage < 1){
				$prevpage = 1;
			}
			//下一页
			$nextpage = $page + 1;
			if($nextpage > $cpage){
				$nextpage = $cpage;
			}
		?>
		<div style="width:960px;margin:0 auto;">
			共<?php echo $c[0]?>条数据 <?php echo $page?>/<?php echo $cpage?>
		</div>
		<div class="clear"></div>
		<div style="width:960px;margin:0 auto;">
			<a href="list.php?cid=<?php echo $cid?>&page=1">首页</a>
			<a href="list.php?cid=<?php echo $cid?>&page=<?php echo $prevpage?>">上一页</a>
			<a href="list.php?cid=<?php echo $cid?>&page=<?php echo $nextpage?>">下一页</a>
			<a href="list.php?cid=<?php echo $cid?>&page=<?php echo $cpage?>">尾页</a>
		</div>
		<div class="clear"></div>
		<!--底部 友链 版权-->
		<?php 
			include './includes/footer.inc.php';
		?>
	</body>
</html>